/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import web.enity.Book;
import web.service.BookService;
import web.service.Impl.BookServiceImpl;
import web.util.Pager;

/**
 *
 * @author 10592
 */
public class BookServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        //从前端获取页面初始化参数
        int page = 1;//当前页
        int row = 10;//页面行数
        System.out.println(page);
        BookService bookService = new BookServiceImpl();
        int totalCount = bookService.selectCount();//数据总数
        String strPage = request.getParameter("page");//当前页数
        System.out.println(strPage );
        if (strPage != null ) {
            try {
                page = Integer.valueOf(strPage);
            } catch (Exception e) {
                log(e.getMessage(), e);
            }
        }

        List<Book> bl = bookService.selectBooks(page, row);
        Pager pager = new Pager(totalCount, row, page);
        System.out.println("共查询出 " + bl.size() + " 条数据");
        System.out.println("ID\t商品名\t商品描述\t商品价格\t库存");
        for (Book book : bl) {
            System.out.println(book.getBookName() + "\t" + book.getPrice() + "\t" + book.getStock());
        }
        System.out.println("-----------------------------------------");
        // 将模型数据放到request范围
        request.setAttribute("resultList", bl);
        request.setAttribute("pager", pager);
        // 请求转发
        request.getRequestDispatcher("index.jsp").forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
